Makes a new list of just the items of target that the script test says yes to, in their original order. test is the NAME of one of your scripts; it takes one argument and gives back true or false. The new list has the same capacity as target (the most it could ever hold). target is not changed.
list_filter(target, test) -> list<T>| Name | Type |
|---|---|
target | list |
test | script |
list<T>
Makes a new list holding only the items of target, a list, that a script says yes to. The script is asked about each item in turn. It answers true to keep the item and false to leave it out. The items that are kept stay in the order they had. target itself is not changed.
var high = list_filter(scores, is_high);If scores holds 30, 80, 55 and 10 and is_high says yes to 50 or more, high holds 80 and 55.
The second thing you give is the name of one of your scripts: a file in the project's scripts folder with a function named like the file. Write just the name, with no brackets after it. Here is a script that answers whether a score is high:
// scripts/is_high.gbml
function is_high(score: int): bool {
return score >= 50;
}The script has to follow four rules, and each one is checked before the game runs:
score: int. A list of whole numbers fits a parameter that wants decimals, but a list of decimals does not fit one that wants whole numbers. A script that is only used here can leave the type out: it takes the kind of the items of the list. Write it when you want it checked.true or false. Write : bool after the parameters and return true; or return false; in the script. A script that gives back a number, or nothing, is refused.function (n) { return n > 5; } in the middle of the line works as well: it takes the kind of the items of the list for its parameter, and the same rules hold for it.You do not have to make a script file for a short job. Write the function right where the name would go:
var high = list_filter(scores, function (n) { return n >= 50; });It is the script, only without a name. Its one parameter takes the kind of the items of the list, so you do not write the kind (you may, and then it has to fit). It can read and change the variables around it, such as a local you made on the line before, and a return inside it leaves only the function, never the event or script around it. Use a script file instead when the same job is needed in more than one place.
Even when you filter an array or a grid, the answer is a list. How many items pass is only found out while the game runs, and an array's size is fixed when the game is built. A list is the container made for exactly this: it has a fixed capacity and a length that goes up and down. So you ask the answer how many passed with list_length, and you can go on to add to it or take from it like any list.
The new list has the same capacity as the list you filter, because the most that could pass is every item. A list with room for 200 makes a new list with room for 200, even if only 3 items pass. So a filter costs a whole second list's worth of memory. If the list is a variable of an object, every instance of the object carries its own.
For the same reason a filter can only be written in one place: the whole right-hand side of the line that makes the variable, like var high = list_filter(scores, is_high);, or high = list_filter(scores, is_high); in an object's Create event. It cannot go inside another call or a sum, on a line by itself, over a variable that already exists, or in a later event such as Step. To filter again later, empty the list with list_clear and add the items you want one at a time in a loop. If you do not need the original list, list_keep does the same job inside the list itself: it takes out the items the script says no to, needs no second list, and can be written anywhere, which makes it the one to use in a game that filters every frame.
It also needs to know how big the list you filter is, so that has to be a plain variable name whose size is known. A script's parameter could be any size, so it is refused. A filter cannot be given a size of its own either: it always gets the size of the list it filters.
| You do this | What happens |
|---|---|
| filter a list with no items | the new list is empty, and the script is never called |
| the script says no to every item | the new list is empty |
| the script says yes to every item | the new list holds all of them, in the same order |
| a list of text, structs or references | works. Nothing is compared, the script decides |
give a script that is not a script's name, has the wrong number of parameters, the wrong kind of parameter, or does not answer true or false | refused when checked, and the message says which |
| give it an array or a grid | refused when checked, and the message names the function for that kind, like array_filter |
| use it where a value is needed, other than as the whole right-hand side of the line that makes a variable | refused when checked |
One call of your script for each item the list holds, and a second whole list of memory (the capacity of the original). The original is not changed.
/// @type {list<int>}
var scores = [30, 80, 55, 10];
var high = list_filter(scores, is_high); // 80, 55
var how_many = list_length(high); // 2
var best_first = high[0]; // 80A fixed-size vector with the same N as the list (its declared capacity, 8 if untagged), filled by a loop over the list_length(target) items it holds. The answer is a LIST, not an array: how many items pass is only found out while the game runs, and an array's size is fixed when the game is built, whereas a list has a fixed capacity and a length that changes. Its capacity is the original's (the most that could pass is all of them), so it is a second whole container of memory: as many places as the original has room for, not just how many pass. Like a copy, it has to be the whole right-hand side of the line that makes the variable, the original has to be a variable whose size is known (a script's parameter could be any size), and it cannot be given a size of its own. The script is called once for each item, in order, and the items it says yes to are added to the answer in the same order; the original is not changed. The script has to give back true or false, take exactly one argument whose type accepts the items, and be the NAME of one of your scripts: a function written out in the middle of the line is not turned into game code yet, so it is refused instead of doing nothing. O(n) calls of the script, and no timing was measured. A game that filters a list every frame can use list_keep instead, which takes the items out of the list itself and sets no second list aside.